<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            padding-bottom: 800px;
            padding-left: 30px;
        }

        .box {
            width: 500px;
            height: 300px;
            border: 1px solid black;
        }
    </style>
</head>

<body>
    <!-- 
        Promise是一个状态控制器，通过三种状态控制分支代码执行，当状态为成功执行then的第一个函数，当状态为失败执行then的第二个函数或者catch函数
        因为promise分支代码的执行由状态变换控制，所以常被同于异步回调的链式方式操作方案，再结合 async 和 await 可以构建出类同步代码
    -->
    <div id="app">
        <div class="box" ref="chart"></div>
        <hr>
        <pre>{{ types }}</pre>
        <hr>
        <ol>
            <li v-for="t in homeData">
                <div>
                    {{ t.typeName }} 
                    【<a :href="`./列表页面.html?id=${t.id}&type=${t.typeName}`">更多</a>】 
                </div>
                <ul>
                    <li v-for="f in t.list">
                        <a :href="`./详情页面.html?id=${f.id}`">{{ f.filmName }}</a>
                    </li>
                </ul>
            </li>
        </ol>
        <pre>{{ homeData }}</pre>
    </div>
    <script src="../assets/qs.js"></script>
    <script type="module">
        import * as echarts from "../assets/echarts/echarts.esm.min.js";
        import axios from "../assets/axios-es/axios.min.js"
        // 加载 Vue3 的ESM 模块文件
        import { createApp } from "../assets/vue3/vue.esm-browser.js"
        createApp({
            data() {
                return {
                    types:[],
                    homeData:[]
                }
            },
            methods: {
                loadTypes(){
                    axios.get("http://localhost:3000/types")
                        .then(( { data } )=>{
                            this.types = data;
                        })
                },
                loadHomeDataTemp(){
                    axios.get("http://localhost:3000/types")
                        .then(( { data } )=>{
                            this.homeData = data;
                            let parr = []
                            for (let item of data) {
                                let p = axios.get("http://localhost:3000/films",{
                                    params:{
                                        typeId:item.id,
                                        _page:1,
                                        _limit:4
                                    }
                                })
                                // .then(( response )=>{
                                //     item.list = response.data
                                // })
                                parr.push(p)
                            }
                            // this.homeData = data;
                            // 将多个promise对象整合成一个promise对象
                            // console.log(parr);
                            return Promise.all(parr)
                            // Promise.reject()
                            // Promise.resolve()
                            // Promise.all(); // 所有成功才成功
                            // Promise.any() // 一个成功就为成功
                            // Promise.allSettled(); // 是否都结束
                            // Promise.race() // 返回的新Promise状态与最先完成的Promise状态保持一致
                        })
                        .then(( resultList )=>{
                            console.log(resultList)
                            for (let i in resultList) {
                                this.homeData[i].list = resultList[i].data
                            }
                        })
                },
                async loadHomeData(){
                    try {
                        let { data } = await  axios.get("http://localhost:3000/types");
                        for (let type of data) {
                            let response = await axios.get("http://localhost:3000/films/",{
                                params:{
                                    typeId:type.id,
                                    _page:1,
                                    _limit:4
                                }
                            })
                            type.list = response.data
                        }
                        this.homeData = data;
                    } catch (error) {
                        
                    }
                },
                loadReport() {
                    axios.get("http://localhost:3000/report")
                        .then((response)=>{
                            // console.log(response.data)
                            this.setEcharts(response.data)
                        })
                },
                setEcharts(option) {
                    if(!this.$myChart){
                        this.$myChart = echarts.init(this.$refs.chart)
                    }
                    this.$myChart.setOption(option)
                }
            },
            created () {
                // this.loadTypes();
                // this.loadHomeDataTemp()
                this.loadHomeData()
                console.log( location.search );
                console.log( Qs.parse( location.search.replace(/^\?/,"") ) )
            },
            mounted() {
                this.loadReport();
            },
        }).mount("#app")


        // axios.get("http://localhost:3000/films/26636712")
    </script>
</body>

</html>